package com.loe.mvp

import android.app.Activity
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.loe.mvp.ext_java.delay
import com.loe.mvp.initer.BaseIniter
import com.loe.mvp.initer.BaseLoading
import com.loe.mvp.initer.BaseToast
import com.loe.mvp.util.BusMessage
import com.loe.mvp.util.CLOSE_PAGE
import com.loe.mvp.util.UPDATE_PAGE
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseFragment : Fragment(), BaseView
{
    private lateinit var mToast: BaseToast
    private lateinit var mLoading: BaseLoading

    protected lateinit var inflater: LayoutInflater

    protected var isInit = false

    protected lateinit var rootView: View

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        this.inflater = inflater
        rootView = inflater.inflate(getLayout(), container, false)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
        mToast = BaseToast(activity!!)
        mLoading = BaseLoading(activity!!)

        BaseIniter.onBaseView?.onCreate(this)

        runCatching { EventBus.getDefault().register(this) }

        onCreated(savedInstanceState)
    }

    open fun getLayout(): Int
    {
        return -1
    }

    abstract fun onCreated(savedInstanceState: Bundle?)

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBusMessage(message: BusMessage)
    {
        if (message.path == path)
        {
            when (message.msg)
            {
                // 处理所有页面的关闭消息
                CLOSE_PAGE -> finish()
                // 处理所有页面的更新消息
                UPDATE_PAGE -> loadData()
            }
            BaseIniter.onBaseView?.onBus(this, message.msg)
            onBus(message.msg)
        }
    }

    override val root: Activity get() = activity!!
    override val toast: BaseToast get() = mToast
    override val loading: BaseLoading get() = mLoading

    open fun onShow(isResume: Boolean = false)
    {
    }

    open fun onHide(isPause: Boolean = false)
    {
    }

    override fun onHiddenChanged(hidden: Boolean)
    {
        if (hidden)
        {
            onHide()
        } else
        {
            onShow()
        }
    }

    override fun onResume()
    {
        super.onResume()
        BaseIniter.onBaseView?.onResume(this)
        onShow(true)

        if (!isInit)
        {
            delay()
            {
                onInit()
                isInit = true
            }
        }
    }

    protected open fun onInit()
    {
    }

    override fun onPause()
    {
        onHide(true)
        super.onPause()
    }

    override fun onDestroy()
    {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        BaseIniter.onBaseView?.onDestroy(this)
    }
}